package com.wx.app.mylib.app;

import android.content.Context;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.Log;

import com.google.zxing.integration.android.IntentIntegrator;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by Administrator on 2016/5/12.
 */
public class Globals {
    public final static String domain = "http://192.168.1.58:9081/union";
    public final static String domain_airplane = "http://192.168.1.58:9081/union";

    public static void log(Object args) {
        if (args instanceof Exception) {
            Exception e = (Exception) args;
            Log.e("==36999==:", e.getMessage(), e);
        } else {
            Log.i("==36999==:", "" + args);
        }
    }

    public static String getStringDate(Date currentTime) {
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(currentTime);
        return dateString;
    }

    //手机验证
    public static boolean isMobile(String mobile) {
        if (TextUtils.isEmpty(mobile))
            return false;
        String check = "^1\\d{10}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(mobile);
        return matcher.matches();
    }

    //身份证验证
    public static boolean isLegalCard(String legalCard) {
        if (TextUtils.isEmpty(legalCard))
            return false;
        String check = "(^\\d{15}$)|(^\\d{17}([0-9]|X))$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(legalCard);
        return matcher.matches();
    }

    //获取设备id
    public static String getDeviceID(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return tm.getDeviceId();
    }

    /**
     * 生成图片
     *
     * @param context
     * @param drawableName 名称
     * @return
     */
    public static int createDrawableByIdentifier(Context context,
                                                 String drawableName) {
        String pkgName = context.getPackageName();
        return context.getResources()
                .getIdentifier(drawableName, "drawable", pkgName);
    }

    public static String getDeviceName() {
        String name = android.os.Build.MODEL;
        String manufacturer = android.os.Build.MANUFACTURER;
        if (name.indexOf(manufacturer) >= 0) {
            return name;
        } else {
            return manufacturer + " " + name;
        }
    }

    //邮箱验证
    public static boolean isEmail(String textStr) {
        String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern regex = Pattern.compile(check);
        Matcher matcher = regex.matcher(textStr);
        return matcher.matches();
    }

    //密码验证
    public static boolean isPassWord(String textStr) {
        Pattern upperCase = Pattern.compile("[A-Z]");
        Pattern lowerCase = Pattern.compile("[a-z]");
        Pattern numbers = Pattern.compile("[0-9]");
        int capitalletters = 0;
        int loweletters = 0;
        int number = 0;
        if (upperCase.matcher(textStr).find()) {
            capitalletters = 1;
        } else {
            capitalletters = 0;
        }

        if (lowerCase.matcher(textStr).find()) {
            loweletters = 1;
        } else {
            loweletters = 0;
        }

        if (numbers.matcher(textStr).find()) {
            number = 1;
        } else {
            number = 0;
        }

        int total = capitalletters + loweletters + number;
        return (total == 3);
    }

    //密码验证
    public static boolean isPassWord2(String textStr) {
        Pattern pattern = Pattern.compile("^[A-Za-z0-9]{6,12}");
        Matcher matcher = pattern.matcher(textStr);
        if (matcher.matches()) {
            return true;
        } else {
            return false;
        }
    }

    public static String getAccountId(Context context) {
        return new LocalData(context).getString(LocalData.CacheKey.accountId);
    }

    public static String escapeHtml(String content) {
        if (content == null) {
            return "";
        }
        String html = content;
        html = html.replace("'", "&apos;");
        html = html.replaceAll("&", "&amp;");
        html = html.replace("\"", "&quot;");
        html = html.replace("\t", "&nbsp;&nbsp;");
        html = html.replace(" ", "&nbsp;");
        html = html.replace("<", "&lt;");
        html = html.replaceAll(">", "&gt;");
        return html;
    }

    public static void ZxingScan(BaseActivity mActivity) {
        IntentIntegrator integrator = new IntentIntegrator(mActivity);
        integrator.setCaptureActivity(MyCaptureActivity.class);
        integrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
        integrator.setPrompt("Scan something");
        integrator.setOrientationLocked(false);
        integrator.setBeepEnabled(false);
        integrator.initiateScan();

    }
}
